package cn.kgc.controller;

import cn.kgc.pojo.User;
import cn.kgc.service.LoginService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class LoginController {

    @Resource
    LoginService loginService;


    @RequestMapping("login")
    public String doLogin(
            @RequestParam("username") String username,
            @RequestParam("userpassword") String userpassword,
            Model model,
            HttpSession session) {
        User user = loginService.login(username, userpassword);
        if (user == null) {
            model.addAttribute("login", username);
            model.addAttribute("error", "账号或密码错误");
            return "index";
        }
        session.setAttribute("login", user);
        session.setAttribute("logins", user.getId());
        return "redirect:/jsp/body/index.jsp";
    }


    //注册用户
    @RequestMapping("/Add/user")
    public String saveAdmin(User user, Integer pageNum) {
        loginService.AddUser(user);
        //注册成功跳转到登录页面
        return "index";
    }


    //退出
    @RequestMapping("login/exit")
    public  String  loginexit(HttpSession session){
        //去掉session缓存
        session.invalidate();
        return "index";
    }

    //根据id查询验证手机号 邮箱 数据库中是否存在
    @RequestMapping(value = "/user/id",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String userid(String phone){
        Map<String,Object> map=new HashMap<>();
        User user = loginService.selectUserById(phone);
        if(user!=null){//成功
            map.put("user","no");
        }else{//失败
            map.put("user","yes");
        }
        return JSON.toJSONString(map);
    }

    //注册邮箱
    @RequestMapping("/Add/email")
    public String saveemail(User user, Integer pageNum) {
        loginService.AddUser(user);
        //注册成功跳转到登录页面
        return "index";
    }

    //修改密码
  /*  @RequestMapping("/update/password")
    public String updatePassword(User user){
        loginService.updatePassword(user);
        return "/body/geren/update-Password";
    }*/





//    @RequestMapping(value = "regist",method =RequestMethod.POST)
//    public String regist(User user){
//
//        loginService.AddUser(user);
//        return "redirect:/jsp/index.jsp";
//    }
//
//
//    @RequestMapping("/select/all/user")
//    public String selectalluser(@RequestParam("name")String name
//            , @RequestParam("datatime") Date datatime,
//                                Model model){
//        List<User> allUser = loginService.getAllUser(name, datatime);
//        model.addAttribute("user",allUser);
//        return "/jsp/body/index";
//    }


    /*接下来写邮箱Email*/
    /*        @RequestMapping("email")*/


}
